Welcome to the trillium-macros
crate!
This crate provides derive macros for Handler
, AsyncRead
, AsyncWrite
, and Transport
.
derive(Handler)
This crate currently offers a derive macro for Handler that can be used to delegate Handler behavior to a contained Handler type. Currently it only works for structs, but will eventually support enums as well. Note that it will only delegate to a single inner Handler type.
In the case of a newtype struct or named struct with only a single
field, #[derive(Handler)]
is all that's required. If there is more
than one field in the struct, annotate exactly one of them with
#[handler]
.
As of v0.0.2, deriving Handler makes an effort at adding Handler
bounds to generics contained within the #[handler]
type. It may be
overzealous in adding those bounds, in which case you'll need to
implement Handler yourself.
// for these examples, we are using a `&'static str` as the handler type.
use Handler;
#
;
assert_handler;
;
assert_handler;
assert_handler;
assert_handler;
;
assert_handler;
assert_handler;
assert_handler;
Overriding a single trait function
Annotate the handler with a
trillium::Handler
function name #[handler(overrride = FN_NAME)]
where FN_NAME
is one of
run
, before_send
, name
, has_upgrade
, or upgrade
, and
implement the same signature on Self. The rest of the Handler
interface will be delegated to the inner Handler, but your custom
implementation for the specified function will be called instead.
use Handler;
#
let handler = CustomName ;
assert_eq!;
assert_handler;
Overriding several trait functions
Annotate the handler with any number of
trillium::Handler
function names #[handler(except = [run, before_send, name, has_upgrade, upgrade])]
and implement the trillium Handler signature of that name
on Self.
use Handler;
use Handler;
#
let handler = CustomName ;
assert_handler;
derive(AsyncRead)
and derive(AsyncWrite)
To ease the development of Transport
types, this crate provides proc macros
to derive AsyncRead
and AsyncWrite
and delegate them to a field of the
type.
To delegate all methods of AsyncRead
and AsyncWrite
to the corresponding
methods on a field, mark that field with #[async_io]
:
use BoxedTransport;
use ;
use ;
To delegate AsyncRead
and AsyncWrite
to different fields,
mark the fields with #[async_read]
and #[async_write]
, respectively.
These annotations are optional for structs with a single field.
derive(Transport)
This crate provides a proc macro to derive the Transport
type and delegate it
to a field of the type:
use BoxedTransport;
use ;
use ;
This supports the same mechanism that derive(Handler)
does for overriding
individual methods:
use BoxedTransport;
use ;
use ;